const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


var wxParse = require('../../wxParse/wxParse.js'); //引入


import { AddressElet } from 'addressElet.model.js';
var addElet = new AddressElet();

var area = require('../../data/area');

var p = 0,
    c = 0,
    d = 0;
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    provinceName: [],
    provinceCode: [],
    provinceSelIndex: '',
    cityName: [],
    cityCode: [],
    citySelIndex: '',
    districtName: [],
    districtCode: [],
    districtSelIndex: '',
    showMessage: false,
    messageContent: '',
    showDistpicker: false,
    storageArr: []
  },
  onLoad: function (options) {
    // 载入时要显示再隐藏一下才能显示数据，如果有解决方法可以在issue提一下，不胜感激:-)
    // 初始化数据
    console.log('修改', options);
    var id = options.id;
    var uid = options.uid;
    this.setData({
      id: id,
      uid: uid
    });

    this._loadData();

    this.setAreaData();
  },
  _loadData: function () {
    addElet.getUpdate(this.data.id, data => {
      console.log(data);
      this.setData({
        rowfint: data
      });
    });
  },
  setAreaData: function (p, c, d) {
    var p = p || 0; // provinceSelIndex

    var c = c || 0; // citySelIndex

    var d = d || 0; // districtSelIndex
    // 设置省的数据

    var province = area['100000'];
    var provinceName = [];
    var provinceCode = [];

    for (var item in province) {
      provinceName.push(province[item]);
      provinceCode.push(item);
    }

    this.setData({
      provinceName: provinceName,
      provinceCode: provinceCode
    }); // 设置市的数据

    var city = area[provinceCode[p]];
    var cityName = [];
    var cityCode = [];

    for (var item in city) {
      cityName.push(city[item]);
      cityCode.push(item);
    }

    this.setData({
      cityName: cityName,
      cityCode: cityCode
    }); // 设置区的数据

    var district = area[cityCode[c]];
    var districtName = [];
    var districtCode = [];

    for (var item in district) {
      districtName.push(district[item]);
      districtCode.push(item);
    }

    this.setData({
      districtName: districtName,
      districtCode: districtCode
    });
  },
  changeArea: function (e) {
    p = e.detail.value[0];
    c = e.detail.value[1];
    d = e.detail.value[2];
    this.setAreaData(p, c, d);
  },
  showDistpicker: function () {
    this.setData({
      showDistpicker: true
    });
  },
  distpickerCancel: function () {
    this.setData({
      showDistpicker: false
    });
  },
  distpickerSure: function () {
    this.setData({
      provinceSelIndex: p,
      citySelIndex: c,
      districtSelIndex: d
    });
    this.distpickerCancel();
  },
  savePersonInfo: function (e) {
    var data = e.detail.value;

    if (data.default != true) {
      data.default = 0;
    } else {
      data.default = 1;
    }

    var telRule = /^(13[0-9]|14[5-9]|15[0-9]|166|17[0-8]|18[0-9]|19[1|3|5|8|9])[0-9]{8}$/;

    if (data.name == '') {
      this.showMessage('请输入姓名');
    } else if (data.tel == '') {
      this.showMessage('请输入手机号码');
    } else if (!telRule.test(data.tel)) {
      this.showMessage('手机号码格式不正确');
    } else if (data.province == '') {
      this.showMessage('请选择所在地区');
    } else if (data.address == '') {
      this.showMessage('请输入详细地址');
    } else {
      var uid = data.uid;
      console.log(data);
      addElet.getUpdateAdd(data, data => {
        this.showMessage(' 修改成功');
        tt.navigateTo({
          url: '../addressList/addressList'
        });
      });
    }
  },
  showMessage: function (text) {
    var that = this;
    that.setData({
      showMessage: true,
      messageContent: text
    });
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      });
    }, 3000);
  },
  //分享
  onShareAppMessage: function () {
    return {
      title: '大河票务网小程序',
      desc: '大河票务网-购票安全放心保真!提供演唱会、音乐会、体育赛事、话剧歌剧、音乐节、相声曲艺等门票在线订票服务,订票电话400-800-7000',
      path: ''
    };
  }
});